'''
Created on Sep 28, 2011

@author: oabalbin
'''
import os
from collections import defaultdict
def eline(lines,ofile):
    '''
    /tpp.data/akyocum/NSCLC_ftH358.2011_07_30.215655/NSCLC_H358_FT_B01.XML
    xinteract -OdE -drev -Njuly_P_A460 *.XML
RefreshParser interact-july_P_A460.pep.xml /tpp.data/fasta/refseq47.Hs_plusREV.fa
ProteinProphet interact-july_P_A460.pep.xml interact-july_P_A460.prot.xml
rm *.XML *.png
    
    '''
    tag='july'
    tag2='sept'
    tppOpts="-OdE -drev"
    dbfasta="/tpp.data/fasta/refseq47.Hs_plusREV.fa"
    ofile=open(ofile,'w')    
    lines=open(lines)
    cell_list = defaultdict(list)
    

    for i,l in enumerate(lines):
        f = l.strip('\n').split('/')
        if f[-1].find('Trypsin')>0:
            continue
        
        cell = f[3].split('NSCLC_')[1].split('.')[0].replace('ft','FT_').upper()
        
        if f[3].find('2011_09_') > 0:
            nfile = cell+'_'+str(i)+'_'+f[-1].split('_')[-1]
            t=tag2
        else:
            nfile = cell+'_'+str(i)+'_'+f[-1].split('_')[-1]
            t=tag
        cell_list[t+'_'+cell].append('ln -s '+",".join(f).replace(',','/')+' ./'+nfile+'\n')
        
        
    ofile.write('#!/bin/sh\n')
    for cell, xmls in cell_list.iteritems():
        for f in xmls:
            ofile.write(f)
        
        ofile.write('xinteract %s -N%s *.XML\n'%(tppOpts,cell))
        ofile.write('RefreshParser interact-%s.pep.xml %s\n'%(cell,dbfasta))
        ofile.write("ProteinProphet interact-%s.pep.xml interact-%s.prot.xml\n"%(cell,cell))
        ofile.write("rm *.XML *.png\n")## clean out for next iteration
        ofile.write("\n")
    
    
    ofile.write("ProteinProphet *.pep.xml interact-COMBINED.prot.xml\n")
        
    ofile.close()
lines = '/exds/users/oabalbin/projects/nsclc_kras_dep/flowthrough/ft.xmlfiles.tmp'
ofile = '/exds/users/oabalbin/projects/nsclc_kras_dep/flowthrough/tmp_tppft_xml.sh'
eline(lines,ofile)